package cn.biplam.back.operator.ddl.db2sql;

import java.util.ArrayList;

import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.interfaces.DataOperator;
import cn.biplam.common.opcode.sqlengine.DDLParserCode.SqlRuleInfoCode;
import cn.biplam.common.wrapper.ErrorMessage;

/**
 * DB2SQL转换规则数据访问类，用于从数据库读取db2sql的转换规则
 * @author XiaoRuxing 2009-6-3上午09:19:00
 * @version 1.0
 */
public class SqlRuleInfoDataOperator extends DataOperator{

	public SqlRuleInfoDataOperator() throws ErrorMessage {
		super("schemas");
	}
	
	@Override
	protected OperatorResult _exchange() throws ErrorMessage {
		ArrayList<String> sql=new ArrayList<String>();
		if(opId.equals(SqlRuleInfoCode.Get_Schema)){
			this.getSchema(sql,filter);
			return this.doing(sql,Operator_Type.Select);
		}else if(opId.equals(SqlRuleInfoCode.Get_Class)){
			this.getClass(sql,filter);
			return this.doing(sql,Operator_Type.Select);
		}else if(opId.equals(SqlRuleInfoCode.Get_Class_Param_Type)){
			this.getClassParamType(sql,filter);
			return this.doing(sql,Operator_Type.Select);
		}else if(opId.equals(SqlRuleInfoCode.Get_Next_Step_Schema)){
			this.getNextStepSchemas(sql,filter);
			return this.doing(sql,Operator_Type.Select);
		}else if(opId.equals(SqlRuleInfoCode.Get_Method)){
			this.getMethod(sql,filter);
			return this.doing(sql,Operator_Type.Select);
		}else{
			throw new ErrorMessage("Operator Exchange outport not found:"+ opId+" !");
		}
	}

	/**
	 * 根据schema名称和类型ID查找schema
	 * @return schema名称和类型ID对应的schema
	 * @throws ErrorMessage
	 */
	public void getSchema(ArrayList<String> sql,DataFilter filter) throws ErrorMessage {
		/** 查询语句 */
		StringBuilder sp=new StringBuilder();
		sp.append("select a.schema_id            ");
		sp.append("from schemas a                 ");
		sp.append("where                         ");
		sp.append("     a.schema_name='");
		sp.append(filter.getValue("schema_name"));
		sp.append("' and a.schema_type_id='");
		sp.append(filter.getValue("schema_type_id"));
		sp.append("'");
		sql.add(sp.toString());
	}

	/**
	 * 通过Schema_ID查找Class
	 * @return Schema_ID对应的Class
	 * @throws ErrorMessage
	 */
	public void getClass(ArrayList<String> sql,DataFilter filter) throws ErrorMessage {

		/** 查询类信息 */
		StringBuilder sp=new StringBuilder();
		sp.append("select c.class_name,c.class_id 	  ");
		sp.append("from schema_dtl b join class c     ");
		sp.append("         on b.class_id=c.class_id  ");   
		sp.append("where b.schema_id='");
		sp.append(filter.getValue("schema_id"));
		sp.append("'");
		sql.add(sp.toString());
	}

	/**
	 * 通过Class_id查找Class的初始化参数列表
	 * @return Class的初始化参数列表
	 * @throws ErrorMessage
	 */
	public void getClassParamType(ArrayList<String> sql,DataFilter filter) throws ErrorMessage {
		/** 查询参数类型 */
		StringBuilder sp=new StringBuilder();
		sp.append("select c.type_id                             ");
		sp.append("from class a join class_param b              ");
		sp.append("           on a.class_id=b.class_id          ");
		sp.append("      join param c on b.param_id=c.param_id  ");
		sp.append("where a.class_id='");
		sp.append(filter.getValue("class_id"));
		sp.append("'");
		sql.add(sp.toString());
	}

	/**
	 * 查询下一步应该运行的schema
	 * @return 下一步应该运行的schema
	 * @throws ErrorMessage
	 */
	public void getNextStepSchemas(ArrayList<String> sql,DataFilter filter) throws ErrorMessage{
		/** 查询下一步应该运行的schema */
		StringBuilder sp=new StringBuilder();
		sp.append("select  a.schema_id        ");
		sp.append("from schemas a              ");
		sp.append("where a.pre_schema_id='");
		sp.append(filter.getValue("pre_schema_id"));
		sp.append("'                          ");
		sp.append("order by a.schema_seq      ");
		sql.add(sp.toString());
	}

	/**
	 * 通过classId查找方法名
	 * @return 查找classId对应的方法名
	 * @throws ErrorMessage
	 */
	public void getMethod(ArrayList<String> sql,DataFilter filter) throws ErrorMessage{

		StringBuilder sp=new StringBuilder();
		sp.append("select c.method_name                             ");
		sp.append("from schema_dtl a join schema_dtl_act b          ");
		sp.append("             on a.schema_dtl_id=b.schema_dtl_id  ");
		sp.append("       join method c                             ");
		sp.append("             on b.method_id=c.method_id          ");
		sp.append("where a.class_id='");
		sp.append(filter.getValue("class_id"));
		sp.append("'");
		sql.add(sp.toString());
	}
}
